import 'package:erp/data/model/process_node.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:get/get.dart';

class ProcessNodeChooseController extends GetxController {
  final Map<ProcessNode, List<PickerItem>> items = {};

  @override
  void onInit() {
    var data = Get.arguments['data'];
    (data as List).forEach((e) {
      ProcessNode processNode = ProcessNode.fromJson(e['processNode']);
      List<dynamic> originOption = e['orgEmpTreeModelList'];
      items[processNode] = convertList2(originOption);
    });
    super.onInit();
  }

  List<PickerItem> convertList2(List<dynamic>? list) {
    if (list == null) {
      return [];
    }
    return list.map((e) {
      PickerItem item = PickerItem(text: Text(e['name']), value: e['id'], children: convertList2(e['children']));
      return item;
    }).toList();
  }

  void submit() {
    for (var element in items.keys) {
      if (element.nodeEmpId == null || element.nodeOrgId == null) {
        EasyLoading.showError('请选择${element.nodeName}审核人员');
        return;
      }
    }
    Get.back(result: {'processNodeList': items.keys.toList()});
  }
}
